package com.ruoyi.workflow.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.constant.CommonConstants;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.workflow.domain.WfCategory;
import com.ruoyi.workflow.domain.WfCategoryFetcher;
import com.ruoyi.workflow.domain.WfCategoryTable;
import com.ruoyi.workflow.domain.dto.WfCategoryListInput;
import com.ruoyi.workflow.domain.dto.WfCategorySaveInput;
import com.ruoyi.workflow.domain.dto.WfCategoryUpdateInput;
import com.ruoyi.workflow.domain.dto.WfCategoryView;
import com.ruoyi.workflow.service.IWfCategoryService;
import lombok.RequiredArgsConstructor;
import org.babyfish.jimmer.Page;
import org.babyfish.jimmer.sql.JSqlClient;
import org.babyfish.jimmer.sql.ast.mutation.SaveMode;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 流程分类Service业务层处理
 *
 * @author KonBAI
 * @date 2022-01-15
 */

@RequiredArgsConstructor
@Service
public class WfCategoryServiceImpl implements IWfCategoryService {

    private final JSqlClient sqlClient;

    @Override
    public WfCategoryView queryById(String categoryId) {
        return sqlClient.findById(WfCategoryView.class, categoryId);
    }


    @Override
    public List<WfCategoryView> queryList(WfCategoryListInput category) {
        WfCategoryTable table = WfCategoryTable.$;
        return sqlClient.createQuery(table)
                .where(table.categoryName().ilikeIf(category.getCategoryName()),
                        table.code().eqIf(category.getCode()))
                .select(table.fetch(WfCategoryView.class))
                .execute();
    }

    @Override
    public int insertCategory(WfCategorySaveInput categoryBo) {
        return sqlClient.saveCommand(categoryBo.toEntity())
                .setMode(SaveMode.INSERT_ONLY)
                .execute()
                .getTotalAffectedRowCount();
    }

    @Override
    public int updateCategory(WfCategoryUpdateInput categoryBo) {
        return sqlClient.saveCommand(categoryBo.toEntity())
                .setMode(SaveMode.UPDATE_ONLY)
                .execute()
                .getTotalAffectedRowCount();
    }

    @Override
    public int deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        WfCategoryTable table = WfCategoryTable.$;
        return sqlClient.createDelete(table).where(table.categoryId().in(ids)).execute();
    }


    /**
     * 校验分类编码是否唯一
     *
     * @param code       流程代码
     * @param categoryId 流程分类ID
     * @return 结果
     */

    @Override
    public boolean checkCategoryCodeUnique(String code, String categoryId) {
        WfCategoryTable table = WfCategoryTable.$;
        WfCategory info = sqlClient.createQuery(table)
                .where(table.code().eq(code))
                .select(table.fetch(WfCategoryFetcher.$.allScalarFields()))
                .fetchOneOrNull();
        if (StringUtils.isNotNull(info) && ObjectUtil.notEqual(info.categoryId(), categoryId)) {
            return CommonConstants.NOT_UNIQUE;
        }
        return CommonConstants.UNIQUE;
    }

    @Override
    public Page<WfCategoryView> queryPage(WfCategoryListInput category, PageQuery pageQuery) {
        WfCategoryTable table = WfCategoryTable.$;
        return sqlClient.createQuery(table)
                .where(table.categoryName().ilikeIf(category.getCategoryName()),
                        table.code().eqIf(category.getCode()))
                .select(table.fetch(WfCategoryView.class))
                .fetchPage(pageQuery.getPageNum(), pageQuery.getPageSize());
    }
}

